package com.arm.controller.admin.product;

import com.arm.controller.BaseController;
import com.arm.def.Constant;
import com.arm.module.Product;
import com.arm.module.ProductEntity;
import com.arm.service.ProductEntityService;
import com.arm.service.ProductService;
import com.arm.service.ProductTypeService;
import com.arm.tools.result.ResultObj;
import com.arm.tools.result.ReturnCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lutao on 2017/7/14.
 */
@RequestMapping("/admin/product")
@RestController
public class AdminProductController extends BaseController {
    @Autowired
    ProductService productService;
    @Autowired
    ProductEntityService productEntityService;
    @Autowired
    ProductTypeService productTypeService;
    /**
     * 查询所有产品名称
     *
     * @param request
     * @return
     */
    @RequestMapping("/getProductList")
    public ModelAndView getProductList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("admin/product/getProductList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        PageHelper.startPage(page, pageSize);
        List<Product> productList = productService.getProductList(map);
        PageInfo<Product> pageinfo = new PageInfo<>(productList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean", map);
        return m;
    }


    /**
     * 跳转到产品名称编辑或添加页面
     *
     * @return
     */
    @RequestMapping("/getProduct")
    public ModelAndView getProduct(Long tid) {
        ModelAndView m = new ModelAndView("admin/product/getProduct");
        if (tid != null) {
            Product product = productService.getProduct(new HashMap<String, String>() {
                {
                    put("tid", String.valueOf(tid));
                }
            });
            m.addObject("product", product);
        }
      m.addObject("productType",productTypeService.getProductTypeList(null));

        return m;
    }

    /**
     * 编辑产品名称
     *
     * @param product
     * @return
     */
    @PostMapping(value = "/modifyProduct")
    public String modifyProduct(Product product) {
        ResultObj resultObj = new ResultObj();
        try {
            productService.modifyProduct(product);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    /**
     * 按名称查询该产品
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getProductEntityByName", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getProductByName(HttpServletRequest request) {
        Map<String, String> map = this.showRequestParameters(request);
        ResultObj resultObj = new ResultObj();
        try {
            List<ProductEntity> productEntitys = productEntityService.getProductEntityList(map);
            resultObj.setData(productEntitys);
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }


    /**
     * 按名称查机构
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getProductEntityByName")
    @ResponseBody
    public String getProductEntityByName(HttpServletRequest request) {
        Map<String, String> map = this.showRequestParameters(request);
        ResultObj resultObj = new ResultObj();
        try {
            List<ProductEntity> productEntityList = productEntityService.getProductEntityList(map);
            resultObj.setData(productEntityList);
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
}
